package by.airlanterns.dao;

import by.airlanterns.dto.User;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Sergey
 * Date: 26.01.13
 * Time: 16:41
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class UserDaoImpl extends GenericDaoImpl<User, Integer> {


    @Override
    protected Class<User> getEntityClass() {
        return User.class;
    }

    @SuppressWarnings("unchecked")
    public List<User> getAllUsers() {
        getHibernateTemplate().setMaxResults(0);
        return (List<User>) getHibernateTemplate().find("from " + User.class.getName());

    }

    @SuppressWarnings("unchecked")
    public User selectUserById(Integer userId) {
        return getHibernateTemplate().get(User.class, userId);
    }

    @SuppressWarnings("unchecked")
    public List<User> selectUserByLogin(String login) {
        getHibernateTemplate().setMaxResults(0);
        return (List<User>) getHibernateTemplate().find("from User u where u.login='" + login + "'");
    }

    public List<User> selectsUserByEmail(String email) {
        getHibernateTemplate().setMaxResults(0);
        return (List<User>) getHibernateTemplate().find("from User u where u.email='" + email + "'");
    }


    public User getUserByEmail(String email) {
        List<User> users = (List<User>) getHibernateTemplate().find("from " + User.class.getName() + " where email='" + email + "'");
        if (users != null) {
            return users.get(0);
        }
        return new User();
    }

}